<template>
  <el-dialog :visible.sync="visible" width="60%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="断面名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入断面名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="断面类型" prop="type">
<!--            <el-select v-model="form.type"  placeholder="请选择">
            <el-option
              v-for="item in dmlxOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>-->
            <el-select v-model="dmlx"  multiple collapse-tags placeholder="请选择">
              <el-option
                v-for="item in dmlxOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监测类型" prop="targetType">
            <el-select v-model="form.targetType"  placeholder="请选择">
              <el-option label="流量" value="流量"></el-option>
              <el-option label="水位" value="水位"></el-option>
          </el-select>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="行政分区" prop="areaCode">
            <el-cascader ref="areaCodeRef" v-model="form.areaCode" :options="areaTree"  :props="{expandTrigger: 'hover',label: 'name', value: 'areaCode', checkStrictly: true}" placeholder="请选择" clearable @change="handleAreaCodeChange"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="流域分区" prop="basinCode">
            <el-cascader ref = "basinCodeRef" v-model="form.basinCode" :options="basinOptions" :props="{expandTrigger: 'hover',label: 'name', value: 'basinCode', checkStrictly: true}" placeholder="请选择" clearable @change="handleBasinCodeChange"></el-cascader>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="经度" prop="longitude">
            <el-input v-model="form.longitude" placeholder="请输入经度" type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纬度" prop="latitude">
            <el-input v-model="form.latitude" placeholder="请输入纬度" type="number"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="警告信息" prop="warn">
            <el-input type="textarea" v-model="form.warn" placeholder="请输入警告信息"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {
        monitoringTime: ''
      },
      dmlx:[],
      dmlxOptions: [
        {
          value: '重点河湖',
          label: '重点河湖'
        }, {
          value: '先行先试',
          label: '先行先试'
        }, {
          value: '水量分配',
          label: '水量分配'
        }],
      rules: {
        areaCode: [{ required: true, message: '请选择行政分区', trigger: 'blur' }],
        basinCode: [{ required: true, message: '请选择流域分区', trigger: 'blur' }],
        name: [{ required: true, message: '请输入断面名称', trigger: 'blur' }],
        riverId: [{ required: true, message: '请输入河流id', trigger: 'blur' }],
        type: [{ required: true, message: '请选择断面类型', trigger: 'blur' }],
        longitude: [{ required: true, message: '请输入经度', trigger: 'blur' }],
        latitude: [{ required: true, message: '请输入纬度', trigger: 'blur' }],

      },
      type: ''
    };
  },
  computed: {
    ...mapState(["areaTree", "basinOptions"]),
  },
  methods: {
    showModal(type, row) {
      this.visible = true
      this.type = type
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "dictName": "",
          "remark": "",
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
    // 详情
    getInfoInit(id) {
      this.$api.getEcologicalSectionInfo({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (Array.isArray(this.form.areaCode)) {
              this.form.areaCode = parseInt(this.form.areaCode.slice(-1)[0])
          }
          if (Array.isArray(this.form.basinCode)) {
             this.form.basinCode = parseInt(this.form.basinCode.slice(-1)[0])
          }
          if (this.dmlx.length > 0) {
            this.form.type=this.dmlx[0]
            for(var i=1;i<this.dmlx.length;i++ ){
              this.form.type=this.form.type+','+this.dmlx[i]
            }
          }else{
            this.form.type=''
          }
          if (this.type == 'add') {
            this.$api.addEcologicalSection(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateEcologicalSection(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
    handleAreaCodeChange(selected){
      this.$refs.areaCodeRef.dropDownVisible = false;
      if(selected.length===0){
        this.areaChecked = []
        this.form.areaCode=0
      }
    },

    handleBasinCodeChange(selected){
      this.$refs.basinCodeRef.dropDownVisible = false;
      if(selected.length===0){
        this.basinChecked = []
        this.form.basinCode=0
      }
    },
    handleDmlxChange(selected){
      if(selected.length===0){
        this.dmlx = []
        this.form.type=''
      }
    },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
